﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class SAN_PHAMDAL : KetNoiDuLieu
    {        
         public void Insert(SAN_PHAM sanpham)
         {
             SqlCommand cm = new SqlCommand("sp_sanpham_Insert",LayKetNoi());
             cm.CommandType = CommandType.StoredProcedure;
             cm.Parameters.AddWithValue("@HINH_DAI_DIEN", sanpham.Hinhdaidien);
             cm.Parameters.AddWithValue("@TEN_SP", sanpham.Tensp);
             cm.Parameters.AddWithValue("@MA_LOAI_SP", sanpham.Maloaisp);
             cm.Parameters.AddWithValue("@TG_BAT_DAU", sanpham.Tgbatdau);
             cm.Parameters.AddWithValue("@TG_KET_THUC", sanpham.Tgketthuc);
             cm.Parameters.AddWithValue("@SO_LUONG", sanpham.Soluong);
             cm.Parameters.AddWithValue("@SO_LUONG_DA_BAN", sanpham.Soluongdaban);
             cm.Parameters.AddWithValue("@MA_TINH", sanpham.Matinh);
             cm.Parameters.AddWithValue("@DIEU_KIEN_AP_DUNG", sanpham.Dkapdung);
             cm.Parameters.AddWithValue("@DIEM_NOI_BAT", sanpham.Diemnoibat);
             cm.Parameters.AddWithValue("@GIA_GOC", sanpham.Giagoc);
             cm.Parameters.AddWithValue("@GIA_BAN", sanpham.Giaban);
             cm.Parameters.AddWithValue("@TRANG_THAI", sanpham.Trangthai);
             cm.ExecuteNonQuery();
         }

         public void Update(SAN_PHAM sanpham)
         {
             SqlCommand cm = new SqlCommand("sp_sanpham_Update", LayKetNoi());
             cm.CommandType = CommandType.StoredProcedure;
             cm.Parameters.AddWithValue("@MASP", sanpham.Masp);
             cm.Parameters.AddWithValue("@HINH_DAI_DIEN", sanpham.Hinhdaidien);
             cm.Parameters.AddWithValue("@TEN_SP", sanpham.Tensp);
             cm.Parameters.AddWithValue("@MA_LOAI_SP", sanpham.Maloaisp);
             cm.Parameters.AddWithValue("@TG_BAT_DAU", sanpham.Tgbatdau);
             cm.Parameters.AddWithValue("@TG_KET_THUC", sanpham.Tgketthuc);
             cm.Parameters.AddWithValue("@SO_LUONG", sanpham.Soluong);
             cm.Parameters.AddWithValue("@SO_LUONG_DA_BAN", sanpham.Soluongdaban);
             cm.Parameters.AddWithValue("@MA_TINH", sanpham.Matinh);
             cm.Parameters.AddWithValue("@DIEU_KIEN_AP_DUNG", sanpham.Dkapdung);
             cm.Parameters.AddWithValue("@DIEM_NOI_BAT", sanpham.Diemnoibat);
             cm.Parameters.AddWithValue("@GIA_GOC", sanpham.Giagoc);
             cm.Parameters.AddWithValue("@GIA_BAN", sanpham.Giaban);
             cm.Parameters.AddWithValue("@TRANG_THAI", sanpham.Trangthai);
             cm.ExecuteNonQuery();
         }

         public void Delete(int masp)
         {
             SqlCommand cm = new SqlCommand("sp_sanpham_Delete", LayKetNoi());
             cm.CommandType = CommandType.StoredProcedure;
             cm.Parameters.AddWithValue("@MASP", masp);
             cm.ExecuteNonQuery();
         }

         public SANPHAMcollection Get(string Where, int PageIndex, int PageSize, string OrderBy,
                                         string OrderDirection, out int TotalRecords)
         {

             SqlCommand cm = new SqlCommand("sp_sanpham_GET", LayKetNoi());
             cm.CommandType = CommandType.StoredProcedure;
             cm.Parameters.AddWithValue("@Where", Where);
             cm.Parameters.AddWithValue("@PageIndex", PageIndex);
             cm.Parameters.AddWithValue("@PageSize", PageSize);
             cm.Parameters.AddWithValue("@OrderBy", OrderBy);
             cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
             SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
             param.Direction = ParameterDirection.Output;
             cm.Parameters.Add(param);
             SqlDataAdapter da = new SqlDataAdapter(cm);
             DataSet ds = new DataSet();
             da.Fill(ds);
             SANPHAMcollection sanphamcollection = new SANPHAMcollection();
             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
             {
                 SAN_PHAM sanpham = new SAN_PHAM();
                 sanpham.Masp = int.Parse(ds.Tables[0].Rows[i]["MASP"].ToString());
                 sanpham.Hinhdaidien = ds.Tables[0].Rows[i]["HINH_DAI_DIEN"].ToString();
                 sanpham.Tensp = ds.Tables[0].Rows[i]["TEN_SP"].ToString();
                 sanpham.Maloaisp = int.Parse(ds.Tables[0].Rows[i]["MA_LOAI_SP"].ToString());
                 sanpham.Tgbatdau = Convert.ToDateTime(ds.Tables[0].Rows[i]["TG_BAT_DAU"].ToString());
                 sanpham.Tgketthuc = Convert.ToDateTime(ds.Tables[0].Rows[i]["TG_KET_THUC"].ToString());
                 sanpham.Soluong = int.Parse(ds.Tables[0].Rows[i]["SO_LUONG"].ToString());
                 sanpham.Soluongdaban = int.Parse(ds.Tables[0].Rows[i]["SO_LUONG_DA_BAN"].ToString());
                 sanpham.Matinh = int.Parse(ds.Tables[0].Rows[i]["MA_TINH"].ToString());
                 sanpham.Dkapdung = ds.Tables[0].Rows[i]["DIEU_KIEN_AP_DUNG"].ToString();
                 sanpham.Diemnoibat = ds.Tables[0].Rows[i]["DIEM_NOI_BAT"].ToString();
                 sanpham.Giagoc = int.Parse(ds.Tables[0].Rows[i]["GIA_GOC"].ToString());
                 sanpham.Giaban = int.Parse(ds.Tables[0].Rows[i]["GIA_BAN"].ToString());
                 sanpham.Trangthai = int.Parse(ds.Tables[0].Rows[i]["TRANG_THAI"].ToString());
                 sanphamcollection.Add(sanpham);
             }
             TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
             return sanphamcollection;
         }
    }
}
